<?php


namespace app\admin\controller;


use think\Controller;
use think\Db;
use app\admin\model\User as UserModel;
use think\facade\Session;

class User extends Controller                  //管理员登录判断
{
    public function login(){
        if(input('')==null) {
            return $this->fetch('user/login');
        }else{
            $user=input('user');//获取前端提交数据
            $psd=input('psd');
            $captcha=input('captcha');
            if($this->check_captcha($captcha)){//判定返回结果是否正确
                if($this->check_login($user,$psd)){
                    session('cj',$user);
                    return json(["res"=>2,"msg"=>'登录成功']);
                }else{
                    return json(["res"=>3,"msg"=>'账户或者密码错误']);
                }
            }else{
                return json(["res"=>1,"msg"=>'验证码错误']);
            }
        }
    }

    public function check_captcha($captcha){//验证验证码是否正确
        if(!captcha_check($captcha)){//判断验证
            return false;
        }else{
            return true;
        }
    }
    public function check_login($user,$psd){//判断账号密码是否正确
        $result=UserModel::where('username',$user)->value('password');
        if($result!=null){
            if($result==md5($psd)){
                return true;
            }else{
                return false;
            }
        }
    }
    public function out(){                   //退出登录
        Session::set('cj','');
        $this->success('退出登录中...','login/user/login');
    }
}